package com.kaigejava.webmaginc;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * 凯哥Java CSDN账号列表
 *
 * @author kaigejava
 */
public class KaiGeJavaCsdn implements PageProcessor {

    public void process(Page page) {
        List<String> list= page.getHtml().xpath("//div[@class='article-list']/div/@data-articleid").all();
        if(!list.isEmpty()) {
            for (String string : list) {
                String articleUrl = "https://blog.csdn.net/kaizi_1992/article/details/"+string;
                System.out.println("==访问文章详情id:"+articleUrl);
                page.addTargetRequest(articleUrl);
            }

        }
        String kaige = "https://blog\\.csdn\\.net/kaizi_1992/article/details/\\d+";
        if(page.getRequest().getUrl().matches(kaige)) {
            System.out.println("标题:"+page.getHtml().xpath("//*[@id=\"mainBox\"]/main/div[1]/div/div/div[1]/h1").toString());

        }

    }

    public Site getSite() {
        return Site.me().setSleepTime(100).setRetryTimes(3);
    }

    public static void main(String[] args) {
        Spider.create(new KaiGeJavaCsdn()).addUrl("https://blog.csdn.net/kaizi_1992").addRequest().run();
    }

}
